var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE_NAME = 'tblSample'; // this should be named in the HTML
var ROW_BASE = 1; // first number (for display)
var hasLoaded = false;
var curr_row = 0;
window.onload=fillInRows2;

function fillInRows(numrows)
{
	var i;
	hasLoaded = true;
	//alert ('Vao ham');
	if (document.getElementById && document.getElementsByTagName) {
		//alert ('Vao if');
		var tbl = document.getElementById(TABLE_NAME);
		var l = tbl.rows.length;
		//alert (l);
		if(l > 1){
			delallrow(tbl, l);
		}
		if(numrows){
			i = 0;
			while(i<numrows){
				addRowToTable2(null,'','','','','','','','');
				//alert (i);
				i++;
				//alert (numrows);
			}
		}
	}
}
function fillInRows2(){
	hasLoaded = true;
}
// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four, five, six,seven,eight)
{
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input checkbox object
	this.four = four; // input radio object
	this.five = five;
	this.six = six;
	this.seven = seven;
	this.eight = eight;
}

/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */

function addRowToTable2(num,RPTID,VNDESC,ENDESC,EFFDATE,EXPDATE)
{	
	//alert(hasLoaded);
	if (hasLoaded && document.getElementById && document.getElementsByTagName) {

	 	//alert('ho tro dom');	
		var tbl = document.getElementById(TABLE_NAME);
	
		var nextRow = tbl.tBodies[0].rows.length;
		
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		// add the row
		
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
		//row.className = 'classy' + (iteration % 2);
	
		// CONFIG: This whole section can be configured
		 //$VNDESC$ENDESC$ISDFT$LSCODE$LSVAL$LSDESC
		// cell 0 - text
		var cell0 = row.insertCell(0);
		cell0.setAttribute('align','center');
		cell0.style.padding = '.0em 0em';
		var txtNum = document.createElement('input');
		txtNum.setAttribute('type', 'text');
		txtNum.setAttribute('name', 'STT' + iteration);
		txtNum.setAttribute('id', 'STT' + iteration);
		txtNum.setAttribute('size', '7');
		txtNum.setAttribute('value', iteration); // iteration included for debug purposes				
		txtNum.setAttribute('readOnly', true);	
		txtNum.className = 'textcenter';
		cell0.appendChild(txtNum);
					
		var cell1 = row.insertCell(1);		
		cell1.style.padding = '.0em 0px';
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', 'RPTID' + iteration);
		txtInp1.setAttribute('id', 'RPTID' + iteration);
		txtInp1.setAttribute('size', '16');
		txtInp1.className = 'textcenter';
		txtInp1.setAttribute('value', RPTID); // iteration included for debug purposes		
		txtInp1.setAttribute('readOnly', true);
		cell1.appendChild(txtInp1);
				
		
		var cell2 = row.insertCell(2);		
		cell2.style.padding = '.0em 0px';
		var txtInp2 = document.createElement('input');
		txtInp2.setAttribute('type', 'text');
		txtInp2.setAttribute('name', 'VNDESC' + iteration);
		txtInp2.setAttribute('id', 'VNDESC' + iteration);
		txtInp2.setAttribute('size', '35');		
		txtInp2.setAttribute('value', VNDESC); // iteration included for debug purposes
		txtInp2.setAttribute('readOnly', true);
		txtInp2.className = 'textleft';
		cell2.appendChild(txtInp2);
		
		var cell3 = row.insertCell(3);
		//cell5.setAttribute('align','center');
		cell3.style.padding = '.0em 0px';
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'text');
		txtInp3.setAttribute('name', 'ENDESC' + iteration);
		txtInp3.setAttribute('id', 'ENDESC' + iteration);
		txtInp3.setAttribute('size', '35');
		txtInp3.setAttribute('value', ENDESC); // iteration included for debug purposes		
		txtInp3.setAttribute('readOnly', true);
		cell3.appendChild(txtInp3);
		
		//======
		var cell4 = row.insertCell(4);
		//cell5.setAttribute('align','center');
		cell4.style.padding = '.0em 0px';
		var txtInp4 = document.createElement('input');
		txtInp4.setAttribute('type', 'text');
		txtInp4.setAttribute('name', 'EFFDATE' + iteration);
		txtInp4.setAttribute('id', 'EFFDATE' + iteration);
		txtInp4.setAttribute('size', '14');
		txtInp4.setAttribute('value', EFFDATE); // iteration included for debug purposes		
		txtInp4.setAttribute('readOnly', true);
		cell4.appendChild(txtInp4);
		
		var cell5 = row.insertCell(5);
		//cell5.setAttribute('align','center');
		cell5.style.padding = '.0em 0px';
		var txtInp5 = document.createElement('input');
		txtInp5.setAttribute('type', 'text');
		txtInp5.setAttribute('name', 'EXPDATE' + iteration);
		txtInp5.setAttribute('id', 'EXPDATE' + iteration);
		txtInp5.setAttribute('size', '14');
		txtInp5.setAttribute('value', EXPDATE); // iteration included for debug purposes		
		txtInp5.setAttribute('readOnly', true);
		cell5.appendChild(txtInp5);
		//======
		//alert("aaaaaaaaaaa");
		var cell6 = row.insertCell(6);
		//cell8.setAttribute('align','center');
		cell6.style.padding = '.0em 2px';
		var btnE2 = document.createElement('input');
		btnE2.setAttribute('type', 'button');
		btnE2.setAttribute('value', '     ');
		btnE2.setAttribute('id','replacement_edit');
		btnE2.onclick = function () {editCurrentRow(this)};
		cell6.appendChild(btnE2);
		
		var cell7 = row.insertCell(7);
		//cell9.setAttribute('align','center');
		cell7.style.padding = '.0em 2px';
		var btnE3 = document.createElement('input');
		btnE3.setAttribute('type', 'button');
		btnE3.setAttribute('value', '     ');
		btnE3.setAttribute('id','replacement_delete');
		btnE3.onclick = function () {deleteCurrentRow(this)};
		cell7.appendChild(btnE3);				
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(txtNum, txtInp1, txtInp2, txtInp3,txtInp4, txtInp5, btnE2, btnE3);
		var textbox = document.getElementById('TOTAL');
		textbox.value = tbl.tBodies[0].rows.length;
	}
}

// CONFIG: this entire function is affected by myRowObject settings

// If there isn't a checkbox in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
//	alert('vao ham');
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		reorderRows(tbl, rIndex);
	}

}
function editCurrentRow(obj)
{
	//alert('vao ham edit');
	var capnhat = document.getElementById('capnhatssrpttype');
	var themmoi = document.getElementById('themmoissrpttype');
	if(capnhat && themmoi){
		capnhat.disabled = false;
		themmoi.disabled = true;
		//alert('vao ham edit1');
		var thisRow = obj.parentNode.parentNode;
		var tbl = thisRow.parentNode.parentNode;
		var rIndex = thisRow.sectionRowIndex;
		curr_row = rIndex;
		//var QGRPCODE =  document.getElementById("QGRPCODE");
		var RPTID =  document.getElementById("RPTID");
		var VNDESC =  document.getElementById("VNDESC");
		var ENDESC =  document.getElementById("ENDESC");
		var EFFDATE =  document.getElementById("EFFDATE");
		var EXPDATE =  document.getElementById("EXPDATE");
							
		if(RPTID){
			RPTID.value = tbl.tBodies[0].rows[rIndex].myRow.two.value;								
			VNDESC.value = tbl.tBodies[0].rows[rIndex].myRow.three.value;
			ENDESC.value = tbl.tBodies[0].rows[rIndex].myRow.four.value;
			EFFDATE.value = tbl.tBodies[0].rows[rIndex].myRow.four.value;
			EXPDATE.value = tbl.tBodies[0].rows[rIndex].myRow.four.value;
		}
	}
	
}

function reorderRows(tbl, startingIndex)
{
	if (hasLoaded) {
						
		var textbox = document.getElementById('TOTAL');
		textbox.value = tbl.tBodies[0].rows.length;
		//alert(tbl.tBodies[0].rows.length);
		if (tbl.rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text
				
				tbl.tBodies[0].rows[i].myRow.two.name = 'RPTID' + count; // input text
				tbl.tBodies[0].rows[i].myRow.three.name = 'VNDESC' + count; // input text
				tbl.tBodies[0].rows[i].myRow.four.name = 'ENDESC' + count; // input text
				tbl.tBodies[0].rows[i].myRow.four.name = 'EFFDATE' + count; // input text
				tbl.tBodies[0].rows[i].myRow.four.name = 'EXPDATE' + count; // input text
				count++;
			}
		}
	}

}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
	
}

function openInNewWindow(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	// set the target to the blank window
	frm.target = 'TableAddRow2NewWindow';
	
	// submit
	frm.submit();
}

function delallrow(moduleTable,l) {
len = l;
	for (i = len -1; i != 0; i--) {
	
		moduleTable.deleteRow(i);
	
	}
}

function addRow(){	
//alert("vao ham");
	var RPTID =  document.getElementById("RPTID");
	var VNDESC =  document.getElementById("VNDESC");
	var ENDESC =  document.getElementById("ENDESC");
	var EFFDATE =  document.getElementById("EFFDATE");
	var EXPDATE =  document.getElementById("EXPDATE");
	
	if(RPTID){
		var RPTIDtmp = RPTID.value;
		var VNDESCtmp = VNDESC.value;
		var ENDESCtmp = ENDESC.value;
		var EFFDATEtmp = EFFDATE.value;
		var EXPDATEtmp = EXPDATE.value;
		
		if(!RPTIDtmp){
			alert('Vui lòng nhập mã loại báo cáo!');	
			return false;
		}
		
	
		if(VNDESCtmp){
			//alert('asd2');
			addRowToTable2(null,RPTIDtmp,VNDESCtmp,ENDESCtmp,EFFDATEtmp,EXPDATEtmp);
			//QGRPCODE.disabled = false;
			RPTID.value="";
			VNDESC.value="";
			ENDESC.value="";	
			return true;
		}else{
			alert('Vui lòng nhập mô tả tiếng việt!');	
			return false;
		}
	}
}

function updateRow(){
	//alert('Vào hàm update');
	var capnhat = document.getElementById("capnhatssrpttype");
	var themmoi = document.getElementById("themmoissrpttype");
	
	if(capnhat && themmoi){
		capnhat.disabled = true;
		themmoi.disabled = false;
		
		var tbl = document.getElementById(TABLE_NAME);
		rIndex = curr_row;
		if(rIndex>=0){
			var RPTID =  document.getElementById("RPTID");
			var VNDESC =  document.getElementById("VNDESC");
			var ENDESC =  document.getElementById("ENDESC");
			var EFFDATE =  document.getElementById("EFFDATE");
			var EXPDATE =  document.getElementById("EXPDATE");
					
			if(RPTID){				
				var RPTIDtmp = RPTID.value;
				var VNDESCtmp = VNDESC.value;
				var ENDESCtmp = ENDESC.value;
				var EFFDATEtmp = EFFDATE.value;
				var EXPDATEtmp = EXPDATE.value;
				
				if(VNDESCtmp){
					tbl.tBodies[0].rows[rIndex].myRow.two.value = RPTIDtmp;				
					tbl.tBodies[0].rows[rIndex].myRow.three.value = VNDESCtmp;
					tbl.tBodies[0].rows[rIndex].myRow.four.value = ENDESCtmp;
					tbl.tBodies[0].rows[rIndex].myRow.four.value = EFFDATEtmp;
					tbl.tBodies[0].rows[rIndex].myRow.four.value = EXPDATEtmp;
					//QGRPCODE.value="";
					RPTID.value="";
					VNDESC.value="";
					ENDESC.value="";	
					return true;
				}else{
					alert('Vui lòng nhập mô tả tiếng việt!');	
					capnhat.disabled = false;
					themmoi.disabled = true;
					return false;
				}			
			}
		}
	}
}